package com.jzy.chatglmsdk18753goldn.session;

import com.jzy.chatglmsdk18753goldn.IOpenAiApi;
import com.jzy.chatglmsdk18753goldn.executor.Executor;
import com.jzy.chatglmsdk18753goldn.executor.aigc.deepseek.DeepSeekExcutor;
import com.jzy.chatglmsdk18753goldn.executor.aigc.qwen.QwenExcutor;
import com.jzy.chatglmsdk18753goldn.executor.aigc.GLM.GLMExecutor;
import com.jzy.chatglmsdk18753goldn.model.Model;
import com.jzy.chatglmsdk18753goldn.utils.BearerTokenUtils;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSources;

import java.util.HashMap;

/**
 * @description 配置文件
 */
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class Configuration {

    @Getter
    @Setter
    private String apiHost;

    @Setter
    @Getter
    private String apiSecretKey;

    private String apiSecretKey_GLM;
    public void setApiSecretKey_GLM(String apiSecretKey_GLM) {
        this.apiSecretKey_GLM = apiSecretKey_GLM;
        String[] arrStr = apiSecretKey_GLM.split("\\.");
        if (arrStr.length != 2) {
            throw new RuntimeException("invalid apiSecretKey");
        }
        this.apiKey = arrStr[0];
        this.apiSecret = arrStr[1];
        this.token = BearerTokenUtils.getGLMToken(apiKey, apiSecret);
    }


    private String apiSecretKey_Qwen;
    public void setApiSecretKey_Qwen(String apiSecretKey_Qwen) {
        this.token = BearerTokenUtils.getQwenToken(apiSecretKey_Qwen);
    }


    @Getter
    private String apiKey;
    @Getter
    private String apiSecret;


    @Getter
    @Setter
    private String token;


    // Api 服务
    @Setter
    @Getter
    private IOpenAiApi openAiApi;

    @Getter
    @Setter
    private OkHttpClient okHttpClient;

    public EventSource.Factory createRequestFactory() {
        return EventSources.createFactory(okHttpClient);
    }

    // OkHttp 配置信息
    @Setter
    @Getter
    private HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.HEADERS;
    @Setter
    @Getter
    private long connectTimeout = 450;
    @Setter
    @Getter
    private long writeTimeout = 450;
    @Setter
    @Getter
    private long readTimeout = 450;

    private HashMap<Model, Executor> executorGroup;

    // http keywords
    public static final String SSE_CONTENT_TYPE = "text/event-stream";
    public static final String DEFAULT_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
    public static final String APPLICATION_JSON = "application/json";
    public static final String JSON_CONTENT_TYPE = APPLICATION_JSON + "; charset=utf-8";

    public HashMap<Model, Executor> newExecutorGroup() {
        this.executorGroup = new HashMap<>();

        // 智谱
        Executor glmExecutor = new GLMExecutor(this);
        this.executorGroup.put(Model.GLM_Z1_AIR, glmExecutor);
        this.executorGroup.put(Model.GLM_Z1_FLASH, glmExecutor);
        this.executorGroup.put(Model.GLM_45_FLASH, glmExecutor);
        this.executorGroup.put(Model.COGVIEW_3, glmExecutor);

        // DeepSeek
        Executor deepseekExecutor = new DeepSeekExcutor(this);
        this.executorGroup.put(Model.DeepSeek_V3, deepseekExecutor);

        // 通义千问
        Executor qwenExecutor = new QwenExcutor(this);
        this.executorGroup.put(Model.Qwen3_Coder_Plus, qwenExecutor);
        return this.executorGroup;
    }

}
